package com.hengpeng.itfinoss.controller.stm;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.persist.entity.OperateLog;
import com.hengpeng.common.persist.entity.OperateLog.OperationType;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.util.CalendarUtil;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.service.ServiceFactory;

@Controller
@RequestMapping("/oplm")
public class OperateLogController extends BaseController
{
	@Autowired
	private ServiceFactory serviceFactory;

	@SuppressWarnings("static-access")
	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request, String userId, String entityName, String entityId,
			String soperateTime1, String soperateTime2, String description, String soperationType)
	{
		map.put("userId", userId);
		map.put("entityName", entityName);
		map.put("entityId", entityId);
		map.put("soperateTime1", soperateTime1);
		map.put("soperateTime2", soperateTime2);
		map.put("description", description);
		map.put("soperationType", soperationType);
		OperationType operationType = null;
		Date operateTime1 = null;
		Date operateTime2 = null;
		if (soperationType != null && soperationType.trim().length() > 0)
			operationType = operationType.valueOf(soperationType);
		if (soperateTime1 != null && soperateTime1.toString().trim().length() > 0)
			operateTime1 = CalendarUtil.parseFormatNoMM(soperateTime1);
		if (soperateTime2 != null && soperateTime2.toString().trim().length() > 0)
			operateTime2 = CalendarUtil.parseFormatNoMM(soperateTime2);
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		try
		{
			LinkedHashMap<String, OrderType> sortMap = new LinkedHashMap<String, OrderType>();
			sortMap.put("id", OrderType.DESC);
			List<OperateLog> operateLogList = serviceFactory.getOperateLogService().selectOperateLogPage(userId,
					entityName, entityId, operateTime1, operateTime2, description, operationType, page.currentPage,
					page.pageSize, sortMap).getReturnValue();
			Long count = serviceFactory.getOperateLogService().selectOperateLogCount(userId, entityName, entityId,
					operateTime1, operateTime2, description, operationType).getReturnValue();
			page.setEntityCount(count.intValue());
			map.put("operateLogList", operateLogList);
			map.put("operateLogListPage", page);
			return "/stm/operatelog/operatelog_list";
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}

	}

	@RequestMapping(value = "/get/{id}")
	public String get(@PathVariable Long id, ModelMap map)
	{
		if (id == null)
			return returnError(map, "操作日志ID不能为空！");
		OperateLog operateLog = serviceFactory.getPersistService().get(OperateLog.class, id);
		if (operateLog == null)
			return returnError(map, "你要查看的操作日志不存在！");
		map.put("operateLog", operateLog);
		return "/stm/operatelog/detail";
	}

}
